<?php 
namespace app\admin\controller;

use think\Controller;
use app\model\Courier;
use think\Db;

class Courierdo extends Controller{

	// 查看快递员列表
	public function getCourierList(){
		$couriers = Courier::getListByPage(10);
		$this->assign("couriers",$couriers);
		return $this->fetch();
	}

	// 查看注册码
	public function registercode(){
		$usenum = Db::name("registercode")->field("count('register_id')")->where("is_use",1)->select();
		$unusenum = Db::name("registercode")->field("count('register_id') unusenum")->where("is_use",0)->find();
		$num = $usenum+$unusenum;
		$codes = Db::name("registercode")->where("is_use",0)->paginate(20);
		$this->assign("usenum",$usenum);
		$this->assign("unusenum",$unusenum["unusenum"]);
		$this->assign("num",$num);
		$this->assign("codes",$codes);

		return $this->fetch();
	}

	// 生成注册码
	public function generateCode(){
		$num = input("num");
		for($i = 0; $i < $num; $i++){
			Db::name("registercode")->insert(['register_code'=>md5(getId())]);
		}

		return $this->redirect('courierdo/registercode');
	}

	// 添加快递员
	public function addCourier(){
		$courier = Courier::get(['courier_name'=>input("courier_password")]);
		if(null == $courier){
			$pars['courier_id'] = getId();
			$pars['courier_name'] = input("courier_name");
			$pars['courier_phone'] = input("courier_phone");
			$pars['courier_password'] = md5(input("courier_password"));
			$courier = Courier::create($pars);
			return getResponse(0,$courier);
		}else{
			return getResponse(1,"该用户已存在");
		}
	}

	// 重置密码
	public function resetPassword(){
		$res = Db::name('courier')->where("courier_id",input('courier_id'))->update(['courier_password'=>md5("123456")]);
			if($res){
				return getResponse(0,input('courier_id'));
			}
			return getResponse(1,"重置密码失败");
	}

	// 禁用快递员
	public function unenableCourier(){
		$courier_id = input("courier_id");
		$courier = Courier::get(input("courier_id"));
		$courier->status = 1;
		$courier->save();
		return getResponse(0,$courier);  
	}

	// 启用快递员
	public function enableCourier(){
		$courier_id = input("courier_id");
		$courier = Courier::get(input("courier_id"));
		$courier->status = 0;
		$courier->save();
		return getResponse(0,$courier); 
	}

	// 查找快递员
	public function findCourier(){
		
	}
}

 ?>